package com.hx.util.img;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.ImageIcon;

import com.hx.util.file.FileUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImgUtil {

	//文件类型列表
	public static final String GIF_TYPE = "image/gif";
	public static final String PNG_TYPE = "image/x-png";
	public static final String JPG_TYPE = "image/jpeg";
	public static final String BMP_TYPE = "image/x-xbitmap";
	public static final String LOCALE_SPLIT = "_";
	public static final String TEXT_TYPE = "text/plain";
	public static final String DOC_TYPE = "application/msword";
	public static final String RAR_TYPE = "application/octet-stream";
	public static final String XLS_TYPE = "application/vnd.ms-excel";
	public static final String ZIP_TYPE = "application/x-zip-compressed";

	/**
	 * 
	 * @param contentType 
	 * @return  文件后缀
	 */
	public static String getPicSuffix(String contentType){
		if((contentType!=null) && (!"".equalsIgnoreCase(contentType))){
			if(GIF_TYPE.equals(contentType)){
				return ".gif";
			}
			if(JPG_TYPE.equals(contentType)){
				return ".jpg";
			}
			if(BMP_TYPE.equals(contentType)){
				return ".bmp";
			}
			if(PNG_TYPE.equals(contentType)){
				return ".png";
			}
			if(TEXT_TYPE.equals(contentType)){
				return ".txt";
			}
			if(DOC_TYPE.equals(contentType)){
				return ".doc";
			}
			if(RAR_TYPE.equals(contentType)){
				return ".rar";
			}
			if(XLS_TYPE.equals(contentType)){
				return ".xls";
			}
			if(ZIP_TYPE.equals(contentType)){
				return ".zip";
			}
		}
		return ".gif";
	}
	
	/**
	 * 
	 * @param contentType
	 * @return 是否图片
	 */
	public static boolean isImage(String contentType){
		if(!GIF_TYPE.equals(contentType) && !JPG_TYPE.equals(contentType) && !PNG_TYPE.equals(contentType)){
			return false;
		}
		return true;
	}
	
	/**
	 * 创建缩略图
	 * @param imageFile
	 * @param thumbnailFile
	 * @param maxWidth
	 * @param maxHeight
	 */
	public static void createThumbnail(File imageFile,String thumbnailFile,int maxWidth,int maxHeight){
		
		OutputStream outputStream = null;
		InputStream inputStream = null;
		
		try {
			Image srcImage = getImageFromFile(imageFile);
			
			int imgWidth = srcImage.getWidth(null);
			int imgHeight = srcImage.getHeight(null);
			
			//Set the scale
			AffineTransform tx = new AffineTransform();
			
			if((imgWidth>maxWidth) || (imgHeight>maxHeight)){
				double scaleX = (double)maxWidth/imgWidth;
				double scaleY = (double)maxHeight/imgHeight;
				double scaleRatio = (scaleX<scaleY) ? scaleX : scaleY;
				
				imgWidth = (int)(imgWidth*scaleRatio);
				imgHeight = (int)(imgHeight*scaleRatio);
				
				//scale as needed
				tx.scale(scaleRatio, scaleRatio);
			}else{
				//and return
				outputStream = new FileOutputStream(thumbnailFile);
				outputStream.write(FileUtil.getFileBytes(FileUtil.getInputStream(imageFile)));
				
				return;
			}
			
			BufferedImage bufferedImage = new BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			
			boolean useTransform = false;
			
			if(useTransform){
				g.drawImage(srcImage, tx, null);
			}else{
				Image scaleImage = getScaledInstance(srcImage, imgWidth, imgHeight);
				g.drawImage(scaleImage, 0, 0, null);
			}
			
			g.dispose();
			
			outputStream = new FileOutputStream(thumbnailFile);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
			encoder.encode(bufferedImage);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(outputStream!=null){
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 创建合适大小的图像
	 * @param imageFile
	 * @param thumbnailFile
	 * @param maxWidth
	 * @param maxHeight
	 */
	public static void createFitSizeThumbnail(File imageFile,String thumbnailFile,int maxWidth,int maxHeight){
		Image srcImage = getImageFromFile(imageFile);
		
		int imgWidth = srcImage.getWidth(null);
		int imgHeight = srcImage.getHeight(null);
		
		boolean accordingWidth = true;
		
		if(imgWidth<imgHeight){
			accordingWidth = false;
		}
		
		int width = 0;
		int height = 0;
		
		if(accordingWidth){
			double fitRatio = (double)maxWidth/imgWidth;
			if(fitRatio>1){
				fitRatio = 1;
			}
			width = maxWidth;
			height = (int)(fitRatio*imgHeight);
		}else{
			double fitRatio =(double) maxHeight/imgHeight;
			if(fitRatio>1){
				fitRatio = 1;
			}
			width = (int)(fitRatio*imgHeight);
			height = maxHeight;
		}
		
		createThumbnail(imageFile,thumbnailFile,width,height);
	}
	
	private static Image getScaledInstance(Image srcImage,int width,int height){
		ImageFilter filter = new java.awt.image.AreaAveragingScaleFilter(width,height);
		ImageProducer prod = new FilteredImageSource(srcImage.getSource(),filter);
		Image newImage = Toolkit.getDefaultToolkit().createImage(prod);
		ImageIcon imageIcon = new ImageIcon(newImage);
		return imageIcon.getImage();
	}
	
	/**
	 * 
	 * @param imageFile
	 * @return 图像文件
	 */
	public static Image getImageFromFile(File imageFile){
		byte[] srcByte = null;
		try {
			srcByte = FileUtil.getFileBytes(FileUtil.getInputStream(imageFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
		ImageIcon imageIcon = new ImageIcon(srcByte);
		
		return imageIcon.getImage();
		
	}
	
}
